package mju.cxf.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mju.cxf.context.BaseContext;
import mju.cxf.entity.Collect;
import mju.cxf.entity.Comments;
import mju.cxf.entity.ProductSpecifications;
import mju.cxf.entity.User;
import mju.cxf.result.Result;
import mju.cxf.service.*;
import mju.cxf.vo.CommentVO;
import mju.cxf.vo.ProductSpecificationsListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "用户商品规格接口列表")
@RequestMapping("/user/api/specifications")
public class IProductSpecificationsController {
    private ProductSpecificationsService productSpecificationsService;

    @Autowired
    public void setProductSpecificationsService(ProductSpecificationsService productSpecificationsService) {
        this.productSpecificationsService = productSpecificationsService;
    }

    /**
     * U分页查询商品信息
     *
     * @return 分页查询结果
     */
    @PostMapping("/pages")
    @ApiOperation(value = "查询商品规格", notes = "用户查询商品规格信息")
    public Result<ProductSpecificationsListVO> getUsersByUsername(@RequestParam(value = "id") int id,
                                                                  @RequestParam(value = "current", defaultValue = "1") int current,
                                                                  @RequestParam(value = "size", defaultValue = "10") int size) {
        return Result.success(productSpecificationsService.userPageBySpecifications(id, current, size));
    }

    /**
     * U差单个商品规格详情页
     *
     * @param id 规格id
     * @return 规格详情信息
     */
    @Autowired
    private SalesService salesService;
    @Autowired
    private CollectionService collectionService;
    @Autowired
    private CommentsService commentsService;
    @Autowired
    private UserService userService;

    @PostMapping("/goodsById")
    @ApiOperation(value = "查询商品详情", notes = "用户查询商品规格详情信息")
    public Result<HashMap<String, Object>> getUsersByUsername(@RequestBody int id) {
        ProductSpecifications productSpecifications = productSpecificationsService.uSelectProductSpecificationsById(id);
        long l = salesService.salesInventory(productSpecifications.getProductId(), productSpecifications.getId());
        HashMap<String, Object> resultData = new HashMap<>();
        resultData.put("id", id);
        resultData.put("goodsInfo", productSpecifications);
        resultData.put("inventory", l);
        final User user = userService.getById(BaseContext.getCurrentId());
        resultData.put("member",user.getMember());
        resultData.put("points",user.getPoints());
        LambdaQueryWrapper<Collect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Collect::getProductId, productSpecifications.getId()).eq(Collect::getUserId, BaseContext.getCurrentId());
        resultData.put("collection", collectionService.getOne(queryWrapper));
        LambdaQueryWrapper<Comments> commentsLqw = new LambdaQueryWrapper<>();

        commentsLqw.eq(Comments::getProductId, productSpecifications.getId()).eq(Comments::getStatus,1).orderByDesc(Comments::getCommentTime);

        final List<CommentVO> commentVOS = commentsService.list(commentsLqw).stream().map(comment -> CommentVO.builder()
                .comment(comment)
                .user(userService.getById(comment.getUserId()))
                .build()).collect(Collectors.toList());
        resultData.put("comments",commentVOS);
        return Result.success(resultData);
    }
}
